package com.jackie.easyble.ui.dialogs

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.jackie.easyble.R
import com.jackie.easyble.databinding.DialogAddAdvertiseDataBinding
import com.sscl.baselibrary.dialog.BaseDataBindingDialog
import com.sscl.baselibrary.textwatcher.HexTextAutoAddEmptyCharInputWatcher
import com.sscl.baselibrary.utils.ToastUtil
import com.sscl.baselibrary.utils.fromHexStringWithSpaceToByteArray
import com.sscl.easyble.advertise.FixedAdvertiseData

/**
 * 选择广播数据类型的对话框
 */
class AddAdvertiseDataDialog(
    context: Context,
    private val confirmButtonClick: (fixedAdvertiseData: FixedAdvertiseData) -> Unit
) :
    BaseDataBindingDialog<DialogAddAdvertiseDataBinding>(context) {

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private val onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            if (position == 0) {
                binding.advertiseDataTypeManufactureDadaContent.visibility = View.VISIBLE
                binding.advertiseDataTypeFullDadaContent.visibility = View.GONE
            } else {
                binding.advertiseDataTypeManufactureDadaContent.visibility = View.GONE
                binding.advertiseDataTypeFullDadaContent.visibility = View.VISIBLE
            }
        }

        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

    }

    /**
     * 点击事件的监听
     */
    private val onClickListener = View.OnClickListener {
        when (it.id) {
            binding.cancelBtn.id -> {
                dismiss()
            }

            binding.confirmBtn.id -> {
                val position = binding.advertiseDataTypeSpinner.selectedItemPosition
                if (position == 0) {
                    val manufactureIdString = binding.manufactureIdEt.text.toString().trim()
                    if (manufactureIdString.isEmpty()) {
                        ToastUtil.toastShort(context, R.string.manufacture_id_empty)
                        return@OnClickListener
                    }
                    val manufactureId = manufactureIdString.toInt()
                    val manufactureDataString = binding.manufactureDadaEt.text.toString().trim()
                    if (manufactureDataString.isEmpty()) {
                        ToastUtil.toastShort(context, R.string.manufacture_data_empty)
                        return@OnClickListener
                    }
                    confirmButtonClick(
                        FixedAdvertiseData(
                            manufactureId,
                            manufactureDataString.fromHexStringWithSpaceToByteArray()
                                ?: return@OnClickListener
                        )
                    )
                } else {
                    val fullDataString = binding.fullDadaEt.text.toString().trim()
                    if (fullDataString.isEmpty()) {
                        ToastUtil.toastShort(context, R.string.full_data_empty)
                        return@OnClickListener
                    }
                    confirmButtonClick(
                        FixedAdvertiseData(
                            fullDataString.fromHexStringWithSpaceToByteArray()
                                ?: return@OnClickListener
                        )
                    )
                }
                dismiss()
            }
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * 抽象方法
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun getLayout(): Int {
        return R.layout.dialog_add_advertise_data
    }

    override fun setBinding() {

    }

    override fun doBeforeInitOthers() {

    }

    override fun initViewData(savedInstanceState: Bundle?) {
        binding.manufactureDadaEt.addTextChangedListener(
            HexTextAutoAddEmptyCharInputWatcher(
                binding.manufactureDadaEt,
                29
            )
        )
        binding.fullDadaEt.addTextChangedListener(
            HexTextAutoAddEmptyCharInputWatcher(
                binding.fullDadaEt,
                31
            )
        )
    }

    override fun initOtherData() {

    }

    override fun initEvents() {
        binding.cancelBtn.setOnClickListener(onClickListener)
        binding.confirmBtn.setOnClickListener(onClickListener)
        binding.advertiseDataTypeSpinner.onItemSelectedListener = onItemSelectedListener
    }

    override fun doAfterAll() {

    }
}